/**
 * Created by mar105 on 2019-02-25.
 */
import { message } from 'antd';
import moment from 'moment';
import * as commonUtils from '../../utils/utils';
import * as commonFunc from './commonFunc'; /* 通用单据方法 */
import * as commonConfig from '../../utils/config';
import * as commonServices from '../../services/services';
import imgCheck from '../../assets/haveChecked.png'; /* 已审核ICON */
import imgInvalid from '../../assets/haveCancel.png'; /* 已作废ICON */

// -----------------------------------------------------------------------

/** 验证表格数据是否正确 */
export function validateTable(configData, validateData) {
  let bResult = true;
  if (commonUtils.isNotEmptyArr(configData) && commonUtils.isNotEmptyArr(validateData)) {
    const notEmptyConfigData = configData.gdsconfigformslave.filter(item => item.bNotEmpty && item.sName !== '');
    for (const row of validateData) {
      for (const item of notEmptyConfigData) {
        if (commonUtils.isEmpty(row[item.sName])) {
          message.warn(`${item.showName}不能为空!`);
          bResult = false;
          break;
        }
      }
      if (!bResult) break;
    }
  }
  return bResult;
}

/** 添加审核或者作废图标 */
export function handleAddIcon(masterData) {
  let imgSrc = '';
  if (commonUtils.isNotEmptyObject(masterData)) {
    const { bInvalid, bCheck } = masterData;
    if (bInvalid) {
      imgSrc = imgInvalid;
    } else if (bCheck) {
      imgSrc = imgCheck;
    }
  }
  return imgSrc;
}

/** 保存数据 */
export async function saveData(param) {
  const { token, value, sModelsId } = param;
  const dataReturn = (await commonServices.postValueService(
    token, value,
    `${commonConfig.server_host}business/addUpdateDelBusinessData?sModelsId=${sModelsId}`,
  )).data;
  /*   成功的话返回数据   */
  if (dataReturn.code === 1) { /*   成功   */
    message.success(dataReturn.msg);
    return dataReturn;
  } else { /*   失败   */
    message.error(dataReturn.msg);
    return false;
  }
}


/** 处理数据格式
 * sTbName 表名
 * saveTmpData 表单数据
 * delTmpData 表单删除数据
 * bAll 不过滤增加数据还是修改数据 */
export function mergeData(name, sTbName, saveTmpData, delTmpData, bAll) {
  const delData = commonUtils.isEmptyArr(delTmpData) ? [] : delTmpData;
  const savesData = commonUtils.isEmptyArr(saveTmpData) ? [] : saveTmpData;
  const returnData = bAll ? savesData : savesData.filter(item => item.handleType === 'add' || item.handleType === 'update');
  return { name, sTable: sTbName, column: [...returnData, ...delData] };
}

/** 处理表格传入数据格式 */
export function getTableTypes(name, props) {
  const returnTypes = {
    name,
    app: props.app,
    formId: props.sModelsId,
    getSqlDropDownData: props.getSqlDropDownData,
    handleSqlDropDownNewRecord: props.handleSqlDropDownNewRecord,
    getFloatNum: props.getFloatNum,
    getDateFormat: props.getDateFormat,
    onDataChange: props.onDataChange,
    onUploadChange: props.onUploadChange,
    onModalCancel: props.onModalCancel,
    onLookRow: props.onDataRowLook,
    onAddRow: props.onDataRowAdd,
    onDelRow: props.onDataRowDel,
    onSelectRowChange: props.onTableSelectRowChange,
    onDoubleClick: props.onDoubleClick,
    selectedRowKeys: props[`${name}SelectedRowKeys`],
    headerColumn: props[`${name}Column`],
    config: props[`${name}Config`],
    data: props[`${name}Data`],
    totalData: props[`${name}Total`],
    enabled: props.enabled,
    rowSelectionType: 'checkbox', // 表格选择是单选还是多选择
    tableProps: {}, // 表格属性
    isKeyUsesId: true, // 表格是否使用sId作为key 如果不传就是随机
    previewImage: props.previewImage, /* 预览图片地址 */
    previewVisible: props.previewVisible, /* 图片预览弹窗 */
  };
  return returnTypes;
}

/**   新增行业务(销售业务)   */
export function doAddRowBusinessSales(masterData, masterConfig) {
  /* 窗体显示配置 */
  const masterShowConfig = commonFunc.getConfigCondition(masterConfig.gdsconfigformslave, 'bVisible');
  /*   客户名称为空时不允许增加行   */
  if (commonUtils.isEmptyObject(masterData) || commonUtils.isEmptyStr(masterData.sCustomerId)) {
    const { showName } = masterShowConfig.filter(item => (item.sName === 'sCustomerId'))[0];
    message.warn(`${showName}不能为空!`);
    return true;
  } else {
    return false;
  }
}

/**   通过主表税率和税率id变更从表税率及税率id   */
export function changeSlaveTaxRateTaxIdByMaster(masterData, addSlaveRow) {
  addSlaveRow.dTaxRate = getMasterTaxRate(masterData);
  addSlaveRow.sTaxId = getMasterTaxId(masterData);
  return addSlaveRow;
}

/**   获取主表税率   */
export function getMasterTaxRate(masterData) {
  /*   返回值声明   */
  let taxRate = 0;
  /*   主表数据存在病情不是undefined的时候返回主表数据的税率值   */
  if (commonUtils.isNotEmptyObject(masterData) && commonUtils.isNotUndefined(masterData.dTaxRate)) {
    taxRate = masterData.dTaxRate;
  }
  /*   返回值   */
  return taxRate;
}

/**   获取主表税率id   */
export function getMasterTaxId(masterData) {
  /*   返回值声明   */
  let taxId = '';
  /*   主表数据存在病情不是undefined的时候返回主表数据的税率值   */
  if (
    commonUtils.isNotEmptyObject(masterData) &&
    commonUtils.isNotEmptyStr(masterData.sTaxId)) {
    taxId = masterData.sTaxId;
  }
  /*   返回值   */
  return taxId;
}

/**   新增行业务(销售订单业务)   */
export function doAddRowBusinessSalesOrder(addSlaveRow, masterData) {
  /*   执行条件   */
  if (
    commonUtils.isNotEmptyObject(masterData) &&
    commonUtils.isNotEmptyObject(addSlaveRow)
  ) {
    /*   交货日期带到从表   */
    addSlaveRow = changeSlaveDeliverDateByMaster(masterData, addSlaveRow);
  }
  /*   返回值   */
  return addSlaveRow;
}

/**   通过主表交货日期变更从表交货日期   */
export function changeSlaveDeliverDateByMaster(masterData, addSlaveRow) {
  if (commonUtils.isUndefinedNull(masterData.tDeliverDate)) {
    addSlaveRow.tDeliverDate = null;
  } else if (typeof masterData.tDeliverDate === 'object') {
    addSlaveRow.tDeliverDate = masterData.tDeliverDate;
  } else {
    addSlaveRow.tDeliverDate = moment(masterData.tDeliverDate);
  }
  return addSlaveRow;
}

/**   新增行业务(销售送货单单业务)   */
export function doAddRowBusinessSalesDeliver(addSlaveRow, masterData) {
  /*   执行条件   */
  if (
    commonUtils.isNotEmptyObject(masterData) &&
    commonUtils.isNotEmptyObject(addSlaveRow)
  ) {
    addSlaveRow = changeSlaveWareHouseIdByMaster(masterData, addSlaveRow);
  }
  /*   返回值   */
  return addSlaveRow;
}

/**   通过主表仓库名称变更从表仓库名称   */
export function changeSlaveWareHouseIdByMaster(masterData, addSlaveRow) {
  if (commonUtils.isUndefined(masterData.sWareHouseId)) {
    addSlaveRow.sWareHouseId = '';
  } else {
    addSlaveRow.sWareHouseId = masterData.sWareHouseId;
  }
  return addSlaveRow;
}

/**   新增行业务   */
export function doAddRowBusiness(addSlaveRow, masterData, masterConfig) {
  /*   返回值   */
  // const { masterConfig } = props;
  const sModelType = 'salesOrder';
  // const { sModelType } = masterConfig;
  /*   销售业务   */
  if (sModelType.startsWith('sales')) {
    /*   客户名称为空时不允许增加行   */
    const flag = doAddRowBusinessSales(addSlaveRow, masterData, masterConfig);
    if (flag) return;
    /*   税率和税率id带到从表   */
    addSlaveRow = this.changeSlaveTaxRateTaxIdByMaster(masterData, addSlaveRow);
    /*   销售订单业务   */
    if (sModelType === 'salesOrder') {
      return doAddRowBusinessSalesOrder(addSlaveRow, masterData);
    }
    /*   销售送货单业务   */
    if (sModelType === 'salesDeliver') {
      return doAddRowBusinessSalesDeliver(addSlaveRow, masterData);
    }
  }
}

/**   输入数量, 价格, 税码, 计算计算金额，不含税金额，不含税价格，税额，外币金额，外币税额   */
export function getCalculateByPrice(app, type, masterData, tableDataRow) {
  /*   执行条件   */
  /*   待用数据赋值   */
  const { dNetPrice, dNetMoney } = app.decimals;
  const dProductQty = commonUtils.convertToNum(tableDataRow[`d${type}Qty`]); /* 数量 */
  const dProductPrice = commonUtils.convertToNum(tableDataRow[`d${type}Price`]); /* 价格 */
  const dTaxRate = commonUtils.convertToNum(tableDataRow.dTaxRate); /* 税率 */
  const dCurrencyRate = commonUtils.convertIsNotNumToNumber1(masterData.dCurrencyRate); /* 汇率 */
  const dProductMoney = dProductQty * dProductPrice; /* 金额 */
  const dProductNoTaxMoney = dProductMoney / (1 + (dTaxRate / 100)); /* 不含税金额 */
  const dProductNoTaxPrice = dProductQty !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价 */
  const dProductTaxMoney = dProductMoney - dProductNoTaxMoney; /* 税额 */
  const dProductForeignMoney = dCurrencyRate !== 0 ? dProductMoney / dCurrencyRate : 0; /* 外币金额 */
  const dProductTaxForeignMoney = dCurrencyRate !== 0 ? dProductTaxMoney / dCurrencyRate : 0; /* 外币税额 */
  /*   数据赋值   */
  tableDataRow[`d${type}Money`] = commonUtils.convertFixNum(dProductMoney, dNetMoney);
  tableDataRow[`d${type}NoTaxMoney`] = commonUtils.convertFixNum(dProductNoTaxMoney, dNetMoney);
  tableDataRow[`d${type}NoTaxPrice`] = commonUtils.convertFixNum(dProductNoTaxPrice, dNetPrice);
  tableDataRow[`d${type}TaxMoney`] = commonUtils.convertFixNum(dProductTaxMoney, dNetMoney);
  tableDataRow[`d${type}ForeignMoney`] = commonUtils.convertFixNum(dProductForeignMoney, dNetMoney);
  tableDataRow[`d${type}TaxForeignMoney`] = commonUtils.convertFixNum(dProductTaxForeignMoney, dNetMoney);
  /*   返回值   */
  return tableDataRow;
}

/**   输入报损数量   */
export function getCalculatedLossMoney(app, tableDataRow, type) {
  /*   执行条件   */
  /*   待用数据赋值   */
  const { dNetMoney } = app.decimals;
  const dLossQty = commonUtils.convertToNum(tableDataRow.dLossQty); /* 报损换算数量 */
  const dProductPrice = commonUtils.convertToNum(tableDataRow[`d${type}Price`]);
  const dLossMoney = dLossQty * dProductPrice;
  /*   数据赋值   */
  tableDataRow.dLossMoney = commonUtils.convertFixNum(dLossMoney, dNetMoney);
  /*   返回值   */
  return tableDataRow;
}

/**   输入数量, 计算箱包   */
export function getCalculatedReserveBags(tableDataRow, type) {
  /*   执行条件   */
  if (commonUtils.isNum(tableDataRow[`d${type}Qty`])) {
    const dProductQty = commonUtils.convertToNum(tableDataRow[`d${type}Qty`]); /* 数量 */
    const dTrunkQty = commonUtils.convertIsNotNumToNumber1(tableDataRow.dTrunkQty); /* 数量/箱 */
    const dPackQty = dTrunkQty !== 0 ? dProductQty / dTrunkQty : 0; /* 包数 */
    const dFractionQty = dTrunkQty !== 0 ? dProductQty - (dPackQty / dTrunkQty) : 0; /* 零头数量 */
    /*   数据赋值   */
    tableDataRow.dPackQty = commonUtils.convertStrToNumberUndefined(dPackQty);
    tableDataRow.dFractionQty = commonUtils.convertStrToNumberUndefined(dFractionQty);
  }
  /*   返回值   */
  return tableDataRow;
}

/**   根据金额，具体计算价格，不含税金额，不含税价格，税额，外币金额，外币税额   */
export function getCalculateMoney(app, masterData, tableDataRow, type) {
  /*   执行条件   */
  /*   待用数据赋值   */
  const { dNetPrice, dNetMoney } = app.decimals;
  const dProductQty = commonUtils.convertToNum(tableDataRow[`d${type}Qty`]); /* 数量 */
  const dProductMoney = commonUtils.convertToNum(tableDataRow[`d${type}Money`]); /* 金额 */
  const dTaxRate = commonUtils.convertToNum(tableDataRow.dTaxRate); /* 税率 */
  const dCurrencyRate = commonUtils.convertIsNotNumToNumber1(masterData.dCurrencyRate); /* 汇率 */
  const dProductPrice = dProductQty !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductNoTaxMoney = dProductMoney / (1 + (dTaxRate / 100)); /* 不含税金额 */
  const dProductNoTaxPrice = dProductQty !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价 */
  const dProductTaxMoney = dProductMoney - dProductNoTaxMoney; /* 税额 */
  const dProductForeignMoney = dCurrencyRate !== 0 ? dProductMoney / dCurrencyRate : 0; /* 外币金额 */
  const dProductTaxForeignMoney = dCurrencyRate !== 0 ? dProductTaxMoney / dCurrencyRate : 0; /* 外币税额 */
  /*   数据赋值   */
  tableDataRow[`d${type}Price`] = commonUtils.convertFixNum(dProductPrice, dNetPrice);
  tableDataRow[`d${type}NoTaxMoney`] = commonUtils.convertFixNum(dProductNoTaxMoney, dNetMoney);
  tableDataRow[`d${type}NoTaxPrice`] = commonUtils.convertFixNum(dProductNoTaxPrice, dNetPrice);
  tableDataRow[`d${type}TaxMoney`] = commonUtils.convertFixNum(dProductTaxMoney, dNetMoney);
  tableDataRow[`d${type}ForeignMoney`] = commonUtils.convertFixNum(dProductForeignMoney, dNetMoney);
  tableDataRow[`d${type}TaxForeignMoney`] = commonUtils.convertFixNum(dProductTaxForeignMoney, dNetMoney);
  return tableDataRow;
}

/**   输入不含税价格, 计算不含税金额，税额，金额，价格，外币金额，外币税额   */
export function getCalculateNoTaxPrice(app, masterData, tableDataRow, type) {
  /*   执行条件   */
  /*   待用数据赋值   */
  const { dNetPrice, dNetMoney } = app.decimals;
  const dProductQty = commonUtils.convertToNum(tableDataRow[`d${type}Qty`]); /* 数量 */
  const dProductNoTaxPrice = commonUtils.convertToNum(tableDataRow[`d${type}NoTaxPrice`]); /* 不含税价 */
  const dTaxRate = commonUtils.convertToNum(tableDataRow.dTaxRate); /* 税率 */
  const dCurrencyRate = commonUtils.convertIsNotNumToNumber1(masterData.dCurrencyRate); /* 汇率 */
  const dProductNoTaxMoney = dProductQty * dProductNoTaxPrice; /* 不含税金额 */
  const dProductTaxMoney = dProductNoTaxMoney * (dTaxRate / 100); /* 税额 */
  const dProductMoney = dProductNoTaxMoney + dProductTaxMoney; /* 金额 */
  const dProductPrice = dProductQty !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductForeignMoney = dCurrencyRate !== 0 ? dProductMoney / dCurrencyRate : 0; /* 外币金额 */
  const dProductTaxForeignMoney = dCurrencyRate !== 0 ? dProductTaxMoney / dCurrencyRate : 0; /* 外币税额 */
  /*   数据赋值   */
  tableDataRow[`d${type}NoTaxMoney`] = commonUtils.convertFixNum(dProductNoTaxMoney, dNetMoney);
  tableDataRow[`d${type}TaxMoney`] = commonUtils.convertFixNum(dProductTaxMoney, dNetMoney);
  tableDataRow[`d${type}Money`] = commonUtils.convertFixNum(dProductMoney, dNetMoney);
  tableDataRow[`d${type}Price`] = commonUtils.convertFixNum(dProductPrice, dNetPrice);
  tableDataRow[`d${type}ForeignMoney`] = commonUtils.convertFixNum(dProductForeignMoney, dNetMoney);
  tableDataRow[`d${type}TaxForeignMoney`] = commonUtils.convertFixNum(dProductTaxForeignMoney, dNetMoney);
  /*   返回值   */
  return tableDataRow;
}

/**   输入不含税金额， 计算不含税价格，税额，金额，价格，外币金额，外币税额   */
export function getCalculateNoTaxMoney(app, masterData, tableDataRow, type) {
  /*   执行条件   */
  /*   待用数据赋值   */
  const { dNetPrice, dNetMoney } = app.decimals;
  const dProductQty = commonUtils.convertToNum(tableDataRow[`d${type}Qty`]);
  const dProductNoTaxMoney = commonUtils.convertToNum(tableDataRow[`d${type}NoTaxMoney`]);
  const dTaxRate = commonUtils.convertToNum(tableDataRow.dTaxRate);
  const dCurrencyRate = commonUtils.convertIsNotNumToNumber1(masterData.dCurrencyRate);
  const dProductNoTaxPrice = dProductQty !== 0 ? dProductNoTaxMoney / dProductQty : 0; /* 不含税价格 */
  const dProductTaxMoney = dProductNoTaxMoney * (dTaxRate / 100); /* 税额 */
  const dProductMoney = dProductNoTaxMoney + dProductTaxMoney; /* 金额 */
  const dProductPrice = dProductQty !== 0 ? dProductMoney / dProductQty : 0; /* 价格 */
  const dProductForeignMoney = dCurrencyRate !== 0 ? dProductMoney / dCurrencyRate : 0; /* 外币金额 */
  const dProductTaxForeignMoney = dCurrencyRate !== 0 ? dProductTaxMoney / dCurrencyRate : 0; /* 外币税额 */
  /*   数据赋值   */
  tableDataRow[`d${type}NoTaxPrice`] = commonUtils.convertFixNum(dProductNoTaxPrice, dNetPrice);
  tableDataRow[`d${type}TaxMoney`] = commonUtils.convertFixNum(dProductTaxMoney, dNetMoney);
  tableDataRow[`d${type}Money`] = commonUtils.convertFixNum(dProductMoney, dNetMoney);
  tableDataRow[`d${type}Price`] = commonUtils.convertFixNum(dProductPrice, dNetPrice);
  tableDataRow[`d${type}ForeignMoney`] = commonUtils.convertFixNum(dProductForeignMoney, dNetMoney);
  tableDataRow[`d${type}TaxForeignMoney`] = commonUtils.convertFixNum(dProductTaxForeignMoney, dNetMoney);
  /*   返回值   */
  return tableDataRow;
}

/**   根据不同的模块，选出具体计算不同模块的计算方式等   */
export function getCalculateAllMoney(app, type, sFieldName, masterData, tableDataRow) {
  /*   待用数据声明   */
  /*   不同的数据进行不同的处理   */
  if (sFieldName === 'dAuxiliaryQty' || sFieldName === `d${type}Qty` || sFieldName === `d${type}Price` || sFieldName === 'sTaxId') {
    tableDataRow = getCalculateByPrice(app, type, masterData, tableDataRow);
    if (sFieldName === `d${type}Price`) {
      tableDataRow = getCalculatedLossMoney(app, tableDataRow, type);
    } else if (sFieldName === `d${type}Qty`) {
      tableDataRow = getCalculatedReserveBags(tableDataRow, type);
    }
  } else if (sFieldName === `d${type}Money`) {
    tableDataRow = getCalculateMoney(app, masterData, tableDataRow, type);
  } else if (sFieldName === `d${type}NoTaxPrice`) {
    tableDataRow = getCalculateNoTaxPrice(app, masterData, tableDataRow, type);
  } else if (sFieldName === `d${type}NoTaxMoney`) {
    tableDataRow = getCalculateNoTaxMoney(app, masterData, tableDataRow, type);
  } else if (sFieldName === 'dLossQty') {
    tableDataRow = getCalculatedLossMoney(app, tableDataRow, type);
  }
  return tableDataRow;
}

/** 根据一系列判断，计算出其相应的单位类型 */
export function handleUnitType(app, resultValue) {
  const { dGramWeight, sMaterialsStyle, sMaterialsUnit } = resultValue;
  const bReel = resultValue.bReel !== undefined ? resultValue.bReel : false;
  const sStyle = sMaterialsStyle !== undefined ? sMaterialsStyle.indexOf('*') : -1;
  let sMUnit = 'other';
  const unitT = commonFunc.showMessage(app.commonConst, 't');
  const unitKg = commonFunc.showMessage(app.commonConst, 'kg');
  const unitM2 = commonFunc.showMessage(app.commonConst, 'm2');
  if (sMaterialsUnit === unitT && !bReel && sStyle > -1 && dGramWeight > 0) {
    sMUnit = 't';
  } else if (sMaterialsUnit === unitKg && !bReel && sStyle > -1 && dGramWeight > 0) {
    sMUnit = 'kg';
  } else if (sMaterialsUnit === unitM2 && !bReel && sStyle > -1) {
    sMUnit = 'm2';
  } else if (sMaterialsUnit === unitM2 && bReel && sStyle > -1) {
    sMUnit = 'm2Reel';
  } else if (sMaterialsUnit === unitT && bReel && sStyle === -1 && dGramWeight > 0) {
    sMUnit = 'tNo';
  } else if (sMaterialsUnit === unitKg && bReel && sStyle === -1 && dGramWeight > 0) {
    sMUnit = 'kgNo';
  } else if ((sMaterialsUnit === unitM2) && bReel && sStyle === -1) {
    sMUnit = 'm2ReelNo';
  }
  return sMUnit;
}


/**   根据数量算出换算数量   */
export function getMaterialsQty(app, tableDataRow) {
  /* 根据库存数量计算其换算数量 - 正算 */
  let {
    dGramWeight, sMaterialsStyle, dAuxiliaryQty, dCoefficient,
  } = tableDataRow;
  dCoefficient = commonUtils.convertIsNotNumToNumber1(dCoefficient); /* 系数 */
  const sType = handleUnitType(app, tableDataRow);

  sMaterialsStyle = sMaterialsStyle === undefined ? '' : sMaterialsStyle;
  dAuxiliaryQty = commonUtils.convertToNum(dAuxiliaryQty);
  let dMaterialsQty = 0;
  dGramWeight = commonUtils.convertToNum(dGramWeight);

  const sMStyleO = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[0] : '';
  const sMStyleT = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[1] !== '' ? sMaterialsStyle.split('*')[1] : 1 : '';
  if (sType === 't' || sType === 'kg') {
    /* 按重量 */
    const multiple = sType === 't' ? 1000000000000 : 1000000000;
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT) * dGramWeight) / multiple).toFixed(6);
  } else if (sType === 'm2') {
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT)) / 1000000).toFixed(6);
  } else if (sType === 'm2Reel') {
    dMaterialsQty = ((dAuxiliaryQty * (sMStyleO * sMStyleT)) / 1000).toFixed(6);
  } else if (sType === 'tNo' || sType === 'kgNo') {
    const multiple = sType === 'tNo' ? 1000000000000 : 1000000000;
    dMaterialsQty = ((dAuxiliaryQty * sMStyleO * dGramWeight) / multiple).toFixed(6);
  } else if (sType === 'm2ReelNo') {
    dMaterialsQty = ((dAuxiliaryQty * sMStyleO) / 1000).toFixed(6);
  } else {
    dMaterialsQty = dAuxiliaryQty * dCoefficient;
  }
  tableDataRow.dMaterialsQty = dMaterialsQty;
  return tableDataRow;
}

/** 计算库存数量--反算 */
export function getAuxiliaryQty(app, tableDataRow) {
  let {
    sMaterialsStyle, dAuxiliaryQty, dMaterialsQty, dGramWeight, dCoefficient,
  } = tableDataRow;
  sMaterialsStyle = sMaterialsStyle === undefined ? '' : sMaterialsStyle;
  dMaterialsQty = commonUtils.convertToNum(dMaterialsQty);
  dGramWeight = commonUtils.convertToNum(dGramWeight);
  dCoefficient = commonUtils.convertIsNotNumToNumber1(dCoefficient); /* 系数 */
  const sType = handleUnitType(app, tableDataRow);

  const sMStyleO = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[0] : '';
  const sMStyleT = sMaterialsStyle !== undefined ? sMaterialsStyle.split('*')[1] !== '' ? sMaterialsStyle.split('*')[1] : 1 : '';
  if (sType === 't' || sType === 'kg') {
    /* 按重量 */
    const multiple = sType === 't' ? 1000000000000 : 1000000000;
    dAuxiliaryQty = ((dMaterialsQty * multiple) / dGramWeight / (sMStyleO * sMStyleT)).toFixed(0);
  } else if (sType === 'm2') {
    dAuxiliaryQty = ((dMaterialsQty * 1000000) / (sMStyleO * sMStyleT)).toFixed(2);
  } else if (sType === 'm2Reel') {
    dAuxiliaryQty = ((dMaterialsQty * 1000) / (sMStyleO * sMStyleT)).toFixed(2);
  } else if (sType === 'tNo' || sType === 'kgNo') {
    const multiple = sType === 'tNo' ? 1000000000000 : 1000000000;
    dAuxiliaryQty = ((dMaterialsQty * multiple) / dGramWeight / sMStyleO).toFixed(0);
  } else if (sType === 'm2ReelNo') {
    dAuxiliaryQty = (dMaterialsQty * 1000) / sMStyleO;
  } else {
    dAuxiliaryQty = dMaterialsQty / dCoefficient;
  }
  tableDataRow.dAuxiliaryQty = dAuxiliaryQty;
  return tableDataRow;
}


/**   根据数量用公式处理   */
export async function getFormulaValue(param) {
  /* 根据库存数量计算其换算数量 - 正算 */
  const {
    token, sModelsId, masterData, tableDataRow, sComputeId,
  } = param;
  let result = '';
  const value = {
    sHandleType: 'getResult',
    replaceData: { /* 数据列集合Map形式 */
      billMaster: masterData, /* 单据主表 */
      billSlave: tableDataRow, /* 单据从表 */
    },
    sFormulaId: sComputeId, /* 计算公式ID */
    sClientType: '1',
  };
  const url = `${commonConfig.server_host}replaceField/checkReplaceField?sModelsId=${sModelsId}`;
  const dataReturn = (await commonServices.postValueService(token, value, url)).data;
  /*   成功的话返回数据   */
  if (dataReturn.code === 1) { /*   成功   */
    result = dataReturn.dataset.rows[0].sResult;
  } else { /*   失败   */
    message.error(dataReturn.msg);
    return false;
  }
  return result;
}

export function getShowTableSelectTypes(name, props, sReturnField) {
  const returnTypes = {
    name,
    app: props.app,
    formId: props.sModelsId,
    data: props[`${name}Data`],
    disabled: !props.enabled,
    sReturnFieldName: sReturnField === undefined ? 'value' : sReturnField,
    onDoubleClick: props.onButtonChange,
  };
  return returnTypes;
}

/** Tree 传入数据格式 */
export function getTreeTypes(name, props) {
  const returnTypes = {
    name,
    app: props.app,
    formId: props.sModelsId,
    checkable: props.checkable === undefined ? true : props.checkable,
    disabled: !props.enabled,
    defaultExpandAll: props.defaultExpandAll ? props.defaultExpandAll : false,
    sKeyName: props[`${name}KeyName`] === undefined ? 'key' : props[`${name}KeyName`],
    sShowName: props[`${name}sShowName`] === undefined ? 'sShowName' : props[`${name}sShowName`],
    treeData: props[`${name}Data`] === undefined ? [] : props[`${name}Data`],
    checkedKeys: props[`${name}CheckedKeys`] === undefined ? [] : props[`${name}CheckedKeys`],
    selectedKeys: props[`${name}SelectedKeys`] === undefined ? [] : props[`${name}SelectedKeys`],
    allCheckKeys: props[`${name}AllCheckKeys`] === undefined ? [] : props[`${name}AllCheckKeys`],
    disabledCheckedKeys: props.disabledCheckedKeys === undefined ? [] : props.disabledCheckedKeys,
    onCheck: props.onTreeCheck.bind(this, name),
    showLine: props.showLine,
    isSearchShow: props.isSearchShow,
  };
  return returnTypes;
}

/** 输入框 传入数据格式 */
export function getWriteTypes(name, props) {
  const returnTypes = {
    componentName: name,
    gdsconfigformmasterId: props.gdsconfigformmasterId,
    uploading: props.uploading,
    ...props,
  };
  return returnTypes;
}
